package com.hotec.PROJECT_NAME.component.exception.exc;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 黑山老妖
 * @date 2018/9/19
 */
public class BizException extends RuntimeException {
    int code = 500;

    public BizException(int code, String msg) {
        super(msg);

        if(code==401){
            HttpServletResponse response = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getResponse();
            response.setStatus(401);
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        request.setAttribute("error",msg);
        this.code = code;
    }

    public BizException(String msg) {
        super(msg);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        request.setAttribute("error",msg);

    }

    public BizException(Exception e) {
        super(e);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}
